package satk.beans.persona;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.DataModel;
import satk.objetos.Persona;

/**
 *
 * @author NightNote
 */
@ManagedBean
@ViewScoped
public class AllPersonasBeans implements Serializable {

    public java.util.List<Persona> fullLista = null;
    public java.util.List<Persona> lista = null;
    public DataModel<Persona> dataModel = null;
    public int startPage = 0;
    public int stepPage = 15;
    public boolean morePages = true;

    public AllPersonasBeans() {
        this.fullLista = new satk.database.ManagerPersona().getAll();
        this.lista = new LinkedList<Persona>();
        
        int startIn = (stepPage * startPage);
        int endIn = (stepPage * (startPage + 1));
        int sizeFull = fullLista.size();
        if (startIn > (sizeFull)) {
            startIn = sizeFull;
            morePages = false;
        }
        if (endIn > sizeFull) {
            endIn = (sizeFull);
            morePages = false;
        }
        this.lista.addAll(this.fullLista.subList(startIn, endIn));
        this.dataModel = new javax.faces.model.ListDataModel<Persona>(lista);
    }
    
    public void recargaLista(){
        this.fullLista = new satk.database.ManagerPersona().getAll();
        this.lista = new LinkedList<Persona>();
        morePages = true;
        this.startPage = -1;
        loadMore();
    }

    public void loadMore() {
        startPage++;
        int startIn = (stepPage * startPage);
        int endIn = (stepPage * (startPage + 1));
        int sizeFull = fullLista.size();
        if (startIn > (sizeFull)) {
            startIn = sizeFull;
            morePages = false;
        }
        if (endIn > sizeFull) {
            endIn = (sizeFull);
            morePages = false;
        }
        this.lista.addAll(this.fullLista.subList(startIn, endIn));
        this.dataModel = new javax.faces.model.ListDataModel<Persona>(lista);
    }

    public List<Persona> getLista() {
        return lista;
    }

    public DataModel<Persona> getDataModel() {
        return dataModel;
    }

    public boolean isMorePages() {
        return morePages;
    }
}
